<?php
namespace App\Console\Commands;


use App\Models\Teambition;
use Illuminate\Support\Facades\DB;

class SyncTeambition extends BaseCommand
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'kaoqin:teambition-user';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'sync teambition user';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {

        $this->info('---');

        $user = teambition_user();

        if (!$user) {
            $this->error('未获取到user数据');
            exit;
        }

        $this->info('获取到teambition user');

        $avatar = collect(getTeams()[0]['hasMembers']);


        if (!$avatar) {
            $this->error('未获取到user头像数据');
            exit;
        }

        $this->info('获取到teambition avatar');

        DB::table('teambition_user')->truncate();

        $this->info('已清空表');

        foreach ($user as $v){
            $teambition = new Teambition();

            $avatarUser = $avatar->where('_id', $v['id'])->first();
            $v['avatar'] = $avatarUser['avatarUrl'];
            $v['teambition_sign'] = $v['id'];
            unset($v['id']);
            $teambition->insert($v);
            $this->info('已添加 id为'. $v['teambition_sign']);
        }


        $this->info('teambition user sync ok');

        $this->info('---');
    }
}
